/**
 * 递归思想，递归遍历链表，判断当前节点val值是否是需要删除的那个
*/

const removeElements = (head, val) => {
  if (head === null) return head
  head.next = removeElements(head.next, val)
  return head.val === val ? head.next : head
}

let head = {
  val: 1,
  next: {
    val: 2,
    next: {
      val: 3,
      next: {
        val: 4,
        next: null
      }
    }
  }
}

console.log(removeElements(head, 3));